// 使用opencv模块部署yolov5-6.0版本

#include "yolo.h"
#include <iostream>
#include<opencv2/opencv.hpp>
#include<math.h>

using namespace std;
using namespace cv;
using namespace dnn;

int main()
{	


	cout<<"start-------------------"<<endl;    
	// string model_path = "../../yolov5/runs/train/exp3/weights/best.onnx";
	string model_path = "../model/best-sim.onnx";

	string img_path = "../../weima/train/images";
    vector<String> imgs;
    // glob：获取文件路径下的所有图片
	glob(img_path, imgs, false);
	// 打乱顺序
	std::random_shuffle(imgs.begin(), imgs.end());
    // std::random_shuffle(imgs.begin(), imgs.end());
	Yolo test;
	Net net;
	
	
	if (test.readModel(net, model_path, false)) {
		cout << "read net ok!" << endl;
	}
	else {
		cout << "read net fail!" << endl;
		return -1;

	}

	//生成随机颜色
	vector<Scalar> color;
	srand(time(0));
	for (int i = 0; i < 80; i++) {
		int b = rand() % 256;
		int g = rand() % 256;
		int r = rand() % 256;
		color.push_back(Scalar(b, g, r));
	}
//------------图片检测--------------------
	
	namedWindow("src",WINDOW_NORMAL);
	namedWindow("detect output",WINDOW_NORMAL);
    resizeWindow("detect output", 1280, 720);
    moveWindow("detect output", 1300, 0);
	for(size_t i=0;i<imgs.size();i++){
		Mat img = imread(imgs[i]);
		resize(img,img,Size(1280,720));
		imshow("src",img);
		vector<Output> result;
    	vector<Output> result_sort_x;

		if (test.Detect(img, net, result)) {
			// test.drawPred(img, result, color);
       		result_sort_x = test.Sort(result);
			test.drawPred(img, result_sort_x, color);
		}
		else {
			cout << "Detect Failed!"<<endl;
		}
    imshow("detect output",img);
	if(waitKey()==27)
		break;
	}
	destroyAllWindows();
    return 0;
}

